#include "movefreedpointview.h"
#include "ui_movefreedpointview.h"
/***************************************************************************
 创建者：          hualei
 开始时间：      2018.09.10
 copyright:(C)    深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "../../programView/programview.h"
#include "movefreeinstructionview.h"

MoveFreeDPointView::MoveFreeDPointView(int styleId,QDialog *parent) :
    QDialog(parent),
    ui(new Ui::MoveFreeDPointView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    // 设置QTableView 数据模型
    dPointValueModel = new QStandardItemModel;
    // 设置列号
    dPointValueModel->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("Index")));
    dPointValueModel->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("MapName")));
    dPointValueModel->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("X")));
    dPointValueModel->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("Y")));
    dPointValueModel->setHorizontalHeaderItem(4, new QStandardItem(QObject::tr("R")));

    // 绑定数据模型
    ui->DPoint_Value_TabView->setModel(dPointValueModel);

    // 设置列宽度 (需要在 setModel 之後設置才有效)
    ui->DPoint_Value_TabView->setColumnWidth(0, 45);        // DPointIndex
    ui->DPoint_Value_TabView->setColumnWidth(1, 150);       // MapName
    ui->DPoint_Value_TabView->setColumnWidth(2, 75);        //    X
    ui->DPoint_Value_TabView->setColumnWidth(3, 75);        //    Y
    ui->DPoint_Value_TabView->setColumnWidth(4, 75);        //    R

    // 设置表头居中
    ui->DPoint_Value_TabView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);

    // 设置不可编辑
    ui->DPoint_Value_TabView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    this->setGeometry(5,50,431,544);
}

void MoveFreeDPointView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->DPoint_Value_TabView->setGeometry(8,40,430,120);
        ui->groupBox->setGeometry(10,165,430,180);

        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->BackMainView_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void MoveFreeDPointView::getInput()
{
        dPointTemp.mapName = dPointValueModel->item(0,1)->text().toStdString();
        if((ui->DPoint_Index_Edit->text().isEmpty()) == false)
        {
            dPointTemp.dpIndex = ui->DPoint_Index_Edit->text().toInt();

            if((ui->DPointPos_X_Edit->text().isEmpty()) == true)
            {
//                dPointTemp.posX = fileData.vehicleDPointList[(dPointTemp.dpIndex -1)].posX;
            }
            else
            {
                dPointTemp.posX = ui->DPointPos_X_Edit->text().toDouble();
            }

            if((ui->DPointPos_Y_Edit->text().isEmpty()) == true)
            {
//                dPointTemp.posY = fileData.vehicleDPointList[(dPointTemp.dpIndex -1)].posY;
            }
            else
            {
                dPointTemp.posY = ui->DPointPos_Y_Edit->text().toDouble();
            }

            if((ui->DPointPos_R_Edit->text().isEmpty()) == true)
            {
//                dPointTemp.posR = fileData.vehicleDPointList[(dPointTemp.dpIndex -1)].posR;
            }
            else
            {
                dPointTemp.posR = ui->DPointPos_R_Edit->text().toDouble();
            }
        }
        else
        {

        }

        testDPoint = dPointTemp;
}

void MoveFreeDPointView::updateView()
{
    ui->DPointPos_R_Edit->clear();
    ui->DPointPos_X_Edit->clear();
    ui->DPointPos_Y_Edit->clear();

    ui->DPoint_Index_Edit->setText(QString::number(dPointTemp.dpIndex));

<<<<<<< HEAD
    dPointValueModel->setItem(0, 0, new QStandardItem(QString::number(dPointTemp.dpIndex)));            // DPoint_Index
    dPointValueModel->setItem(0, 1, new QStandardItem(QString::fromStdString(dPointTemp.mapName)));     // MapName
    dPointValueModel->setItem(0, 2, new QStandardItem(QString::number(dPointTemp.posX/1000.000, 10, 3)));        //    X
    dPointValueModel->setItem(0, 3, new QStandardItem(QString::number(dPointTemp.posY/1000.000, 10, 3)));        //    Y
    dPointValueModel->setItem(0, 4, new QStandardItem(QString::number(dPointTemp.posR, 10, 3)));        //    R
=======
    dPointValueModel->setItem(0, 0, new QStandardItem(QString::number(testDPoint.dpIndex)));            // DPoint_Index
    dPointValueModel->setItem(0, 1, new QStandardItem(QString::fromStdString(testDPoint.mapName)));     // MapName
    dPointValueModel->setItem(0, 2, new QStandardItem(QString::number(testDPoint.posX, 10, 3)));        //    X
    dPointValueModel->setItem(0, 3, new QStandardItem(QString::number(testDPoint.posY, 10, 3)));        //    Y
    dPointValueModel->setItem(0, 4, new QStandardItem(QString::number(testDPoint.posR, 10, 3)));        //    R
>>>>>>> dev

    // 文本居中显示
    dPointValueModel->item(0, 0)->setTextAlignment(Qt::AlignCenter);
    dPointValueModel->item(0, 1)->setTextAlignment(Qt::AlignCenter);
    dPointValueModel->item(0, 2)->setTextAlignment(Qt::AlignCenter);
    dPointValueModel->item(0, 3)->setTextAlignment(Qt::AlignCenter);
    dPointValueModel->item(0, 4)->setTextAlignment(Qt::AlignCenter);
}

void MoveFreeDPointView::changedLanguage()
{
    ui->retranslateUi(this);
}

void MoveFreeDPointView::on_Modif_DPointValue_Affirm_Btn_clicked()
{
//    dPointTemp.clean();
    getInput();
    updateView();
    emit recordEditedDPointSignal(E_RECORD_EDITED_POINT);
}

void MoveFreeDPointView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void MoveFreeDPointView::on_BackMainView_Btn_clicked()
{
    this->close();
    moveFreeInstructionView->show();
}
